package com.pkutech.carcasher.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;

import com.pkutech.carcasher.R;
import com.pkutech.carcasher.content.RoadAdapter;
import com.pkutech.carcasher.content.RoadDataSource;
import com.pkutech.carcasher.model.RoadInfo;
import com.shizhefei.mvc.MVCHelper;
import com.shizhefei.mvc.MVCSwipeRefreshHelper;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 车位搜索
 * Created by cyy on 2016/6/9.
 */
public class ParkingSearchActivity extends BaseActivity {

    public static final int PARK_SEARCH_REQUEST_CODE = 1;
    public static final String PARKING = "PARKING";

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.clear)
    ImageButton clearBtn;
    @BindView(R.id.input)
    EditText inputEditText;
    @BindView(R.id.searchBtn)
    Button searchBtn;
    @BindView(R.id.swipeRefreshLayout)
    SwipeRefreshLayout swipeRefreshLayout;
    @BindView(R.id.listView)
    ListView listView;

    private MVCHelper<List<RoadInfo>> mvcHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_parking_search);
        ButterKnife.bind(this);
        initToolbar(toolbar);

        inputEditText.addTextChangedListener(new TextWatcher() {

            @Override
            public void afterTextChanged(Editable s) {
                String curCharacter = s.toString().trim();

                if (TextUtils.isEmpty(curCharacter)) {
                    clearBtn.setVisibility(View.INVISIBLE);

                } else {
                    clearBtn.setVisibility(View.VISIBLE);

                }
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                                      int count) {
            }

        });

        mvcHelper = new MVCSwipeRefreshHelper<List<RoadInfo>>(swipeRefreshLayout);
        // 设置数据源
        mvcHelper.setDataSource(new RoadDataSource());
        // 设置适配器
        mvcHelper.setAdapter(new RoadAdapter(this));

        // 加载数据
        mvcHelper.refresh();

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                RoadInfo roadInfo = (RoadInfo)view.getTag(R.string.road_item_id);
                Intent intent = new Intent();
                intent.putExtra(PARKING, roadInfo);
                setResult(RESULT_OK, intent);
                finish();
            }
        });
    }

    @OnClick(R.id.searchBtn)
    public void startSearch() {

    }

    @OnClick(R.id.clear)
    public void clearSearchEditText() {
        inputEditText.setText("");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        // 释放资源
        mvcHelper.destory();
    }
}
